package com.keruyun.portal.whitelist.gateway.service;

import com.keruyun.portal.whitelist.gateway.dao.BackServiceInfoMapper;
import com.keruyun.portal.whitelist.gateway.dao.ServiceMapper;
import com.keruyun.portal.whitelist.gateway.dao.WhiteListMapper;
import com.keruyun.portal.whitelist.gateway.entity.*;
import com.keruyun.portal.whitelist.gateway.exceptions.NotDefaultServiceException;
import com.keruyun.portal.whitelist.gateway.exceptions.NotMatchUrlPatternVersion;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * <p>
 * Description: com.keruyun.portal.whitelist.gateway.service
 * </p>
 * date：2018/7/27
 * email:crowhyc@163.com
 *
 * @author Dean.Hwang
 */
@Service
public class PortalWhitelistService {

    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private BackServiceInfoMapper backServiceInfoMapper;
    @Autowired
    private WhiteListMapper whiteListMapper;


    public String getFrontVersion(Long shopId) {
        return getServiceByShopId(shopId).getServiceFrontVer();
    }

    public String getBackInstanceVersion(String routeId, Long shopId) {
        final ServerService serverService = getServiceByShopId(shopId);
        if (StringUtils.isEmpty(serverService.getId())) {
            return "";
        }
        final Optional<BackInstanceGroup> byId = backServiceInfoMapper.findById(serverService.getBackInstanceGroupId());
        return byId.get().getBackInstances().stream().filter(backInstance -> backInstance.getUrlPattern().equalsIgnoreCase(routeId)).findFirst().get().getVersion();

    }

    public VersionInfo getDefaultVersion(String urlPattern) {
        final ServerService byDefTrue = serviceMapper.findByDefTrue();
        if (byDefTrue == null) {
            throw new NotDefaultServiceException();
        }
        final Optional<BackInstanceGroup> byId = backServiceInfoMapper.findById(byDefTrue.getBackInstanceGroupId());
        final Optional<BackInstance> first = byId.get().getBackInstances().stream().filter(backInstance -> backInstance.getUrlPattern().equalsIgnoreCase(urlPattern)).findFirst();

        VersionInfo versionInfo = new VersionInfo();
        versionInfo.setBackVersion(first.orElseThrow(new NotMatchUrlPatternVersion()).getVersion());
        versionInfo.setFrontVersion(byDefTrue.getServiceFrontVer());
        return versionInfo;
    }


    public ServerService getServiceByShopId(Long shopId) {
        final Whitelist byShopIdIn = whiteListMapper.findByShopIdIn(shopId);
        ServerService serverService;
        if (byShopIdIn == null) {
            serverService = serviceMapper.findByDefTrue();
        } else {
            serverService = serviceMapper.findByWhitelistId(byShopIdIn.getId());
        }
        if (serverService == null) {
            return new ServerService();
        }
        return serverService;
    }

}
